// pages/MusicIndex/searchMusicIndex/searchMusicIndex.js
const MusicHttp = require("../../../utils/MusicHttp");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        value: "",
        list: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {

    },

    //封装的搜索事件
    handleSearchMusicHttp: async function (keyWord) {
        var searchRes = await MusicHttp.getRearchKey(keyWord);
        var list = []
        searchRes.data.result.songs.forEach(async (item) => {
            var { id, duration, name } = item;
            var movieTitle = item.album.name;
            var size = item.album.size;
            var imgRes = await MusicHttp.getMusicDetailById(id);
            list.push({
                id,
                duration,
                name,
                movieTitle,
                size,
                imgUrl: imgRes.data.songs[0].al.picUrl
            })
            this.setData({
                list
            })
        })
    },
    //搜索框回车事件
    searchIpt(e) {
        this.handleSearchMusicHttp(e.detail.value);
    },
    //button按钮点击事件
    searchBtn() {
        var value = this.data.value
        this.handleSearchMusicHttp(value);
    },
    //input输入框失去焦点事件
    searchBlurIpt(e) {
        this.setData({
            value: e.detail.value
        })
    },
})